define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: () => {
            const { ref,reactive, onMounted } = Vue
            const index = {
                setup() {
                    const state = reactive({
                        data: [],
                        order: '',
                        sort: '',
                        money:0,
                        filter: {
                            drawer: false,
                            data: {
                                status: 'all',
                                user: { field: 'user_id', value: '' },
                                commission_type: 'all',
                                createtime: [],
                                updatetime: [],
                            },
                            tools: {
                                user: {
                                    type: 'tinputprepend',
                                    label: '用户信息',
                                    placeholder: '请输入查询内容',
                                    value: {
                                        field: 'user_id',
                                        value: '',
                                    },
                                    options: {
                                        data: [{
                                            label: '用户ID',
                                            value: 'user_id',
                                        },
                                            {
                                                label: '用户昵称',
                                                value: 'user.nickname',
                                            },
                                            {
                                                label: '用户手机号',
                                                value: 'user.mobile',
                                            }]
                                    }
                                },
                                createtime: {
                                    type: 'tdatetimerange',
                                    label: '注册时间',
                                    value: [],
                                },
                                oper_type: {
                                    type: 'tselect',
                                    label: '来源',
                                    value: '',
                                    options: {
                                        data: [
                                            {
                                                "name":"平台",
                                                "value":"system",
                                            },
                                            {
                                                "name":"用户",
                                                "value":"user",
                                            }
                                        ],
                                        props: {
                                            label: 'name',
                                            value: 'money_recharge',
                                        },
                                    },
                                },
                                /*flow_id: {
                                    type: 'tselect',
                                    label: '流向',
                                    value: '',
                                    options: {
                                        data: [
                                            {
                                                "name":"平台",
                                                "value":"0",
                                            }
                                        ],
                                        props: {
                                            label: 'name',
                                            value: 'money_recharge',
                                        },
                                    },
                                },*/
                                event: {
                                    type: 'tselect',
                                    label: '类型',
                                    value: '',
                                    options: {
                                        data: [
                                            {
                                                "name":"后台充值",
                                                "value":"admin_recharge",
                                            },
                                            {
                                                "name":"充值",
                                                "value":"money_recharge",
                                            }
                                        ],
                                        props: {
                                            label: 'name',
                                            value: 'money_recharge',
                                        },
                                    },
                                },
                                memo: {
                                    type: 'tinput',
                                    label: '备注',
                                    placeholder: '请输入查询内容（模糊查询）',
                                    value: '',
                                },
                            },
                            condition: {},
                        },
                        statusStyle: {
                            '-1': 'sa-color--danger',
                            0: 'sa-color--info',
                            1: 'sa-color--warning',
                            2: 'sa-color--success',
                        }
                    })

                    // 格式化时间
                    function formatTime(time) {
                        let diffTime = Moment().unix() - time;
                        if (diffTime > 28 * 24 * 60) {
                            return Moment(time * 1000).format('MM/DD HH:mm');
                        }
                        if (diffTime > 360 * 28 * 24 * 60) {
                            return Moment(time * 1000).format('YYYY/MM/DD HH:mm');
                        }
                        return Moment(time * 1000).fromNow();
                    };
                    function getData() {
                        let tempSearch = JSON.parse(JSON.stringify(state.filter.data));
                        let search = composeFilter(tempSearch, {
                            'user.nickname': 'like',
                            'user.mobile': 'like',
                            'memo': 'like',
                            createtime: 'range',
                            updatetime: 'range',
                        });
                        Fast.api.ajax({
                            url: 'shopro/wallet/money/index',
                            type: 'POST',
                            data: {
                                page: pagination.page,
                                list_rows: pagination.list_rows,
                                order: state.order,
                                sort: state.sort,
                                ...search,
                            },
                        }, function (ret, res) {
                            state.data = res.data.data
                            state.money = res.data.money
                            pagination.total = res.data.total
                            return false
                        }, function (ret, res) { })
                    }

                    function onChangeSort({ prop, order }) {
                        state.order = order == 'ascending' ? 'asc' : 'desc';
                        state.sort = prop;
                        getData();
                    }
                    function onOpenFilter() {
                        state.filter.drawer = true
                    }
                    function onChangeFilter() {
                        pagination.page = 1
                        getData()
                        state.filter.drawer && (state.filter.drawer = false)
                    }

                    function onChangeTab() {
                        pagination.page = 1
                        getData()
                    }

                    const pagination = reactive({
                        page: 1,
                        list_rows: 10,
                        total: 0,
                    })

                    const handlePopover = reactive({
                        flag: {}
                    })


                    onMounted(() => {
                        getData()
                    })

                    const exportLoading = ref(false);
                    function onExport(type) {
                        exportLoading.value = true;
                        let tempSearch = JSON.parse(JSON.stringify(state.filter.data));
                        let search = composeFilter(tempSearch, {
                            'user.nickname': 'like',
                            'user.mobile': 'like',
                            'memo': 'like',
                            createtime: 'range',
                            updatetime: 'range',
                        });
                        if (Config.save_type == 'download') {
                            window.location.href = `${Config.moduleurl}/shopro/wallet/money/${type}?page=${pagination.page}&list_rows=${pagination.list_rows}&search=${search.search}`;
                            exportLoading.value = false;
                        } else if (Config.save_type == 'save') {
                            Fast.api.ajax({
                                url: `shopro/wallet/money/${type}`,
                                type: 'GET',
                                data: {
                                    page: pagination.page,
                                    list_rows: pagination.list_rows,
                                    ...search,
                                },
                            }, function (ret, res) {
                                exportLoading.value = false;
                            }, function (ret, res) { })
                        }
                    }
                    return {
                        onClipboard,
                        state,
                        getData,
                        onChangeSort,
                        onOpenFilter,
                        onChangeFilter,
                        onChangeTab,
                        pagination,
                        handlePopover,
                        formatTime,
                        onExport,
                    }
                }
            }
            createApp('index', index);
        },
    };
    return Controller;
});
